package com.rms.server.Controller;

import com.rms.server.Service.CityService;
import com.rms.server.Domain.City;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Tag(name = "城市信息管理", description = "提供城市信息的相关接口")  // 为控制器添加标签和描述
@RestController
public class CityController {

    @Autowired
    private CityService cityService;

    // 使用log4j2进行日志打印
    Logger logger = LogManager.getLogger();

    // 使用Redis
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Operation(summary = "测试接口", description = "返回基本测试信息")  // 添加接口描述
    @GetMapping("/test")
    public Map<String, Object> test() {
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("info", "Hello SpringBoot MyBatis");
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "成功获取基本信息");
        responseInfo.put("data", responseData);

        logger.info("Hello SpringBoot MyBatis Get Test");
        return responseInfo;
    }

    @Operation(summary = "通过城市名称查找城市", description = "根据传入的城市名称返回对应的城市信息")
    @GetMapping("/city")
    public City findCityByName(@RequestParam String name) {
        return cityService.findCityByName(name);
    }

    @Operation(summary = "通过城市ID查找城市", description = "根据传入的城市ID返回对应的城市信息")
    @GetMapping("/city_id")
    public City findCityByID(@RequestParam Long id) {
        return cityService.findCityByID(id);
    }

    @Operation(summary = "Redis 测试接口", description = "使用 Redis 存储并获取键值对")
    @GetMapping("/redis")
    public Map<String, Object> redis_test(@RequestParam String key, @RequestParam String value) {
        redisTemplate.opsForValue().set(key, value);
        String value1 = (String) redisTemplate.opsForValue().get(key);
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("info", "Redis is ok!");
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "Redis测试成功");
        responseInfo.put("data", responseData);

        logger.info("Redis is ok!");
        return responseInfo;
    }
}
//@RestController
//public class CityController {
//    @Autowired
//    private CityService cityService;
//
//
//    // 使用log4j2进行日志打印
//    Logger logger = LogManager.getLogger();
//
//    // 使用Redis
//    @Resource
//    private RedisTemplate<String, Object> redisTemplate;
//
//
//    @GetMapping("/test")
//    public Map test() {
//        HashMap<String, Object> responseInfo = new HashMap<>();
//        HashMap<String, Object> responseData = new HashMap<>();
//        responseData.put("info", "Hello SpringBoot MyBatis");
//        responseInfo.put("code", 20000);
//        responseInfo.put("msg", "成功获取基本信息");
//        responseInfo.put("data", responseData);
//
//        logger.info("Hello SpringBoot MyBatis Get Test");
//        return responseInfo;
//    }
//
//    @GetMapping("/city")
//    public City findCityByName(@RequestParam String name) {
//        return cityService.findCityByName(name);
//    }
//
//    @GetMapping("/city_id")
//    public City findCityByID(@RequestParam Long id) {
//        return cityService.findCityByID(id);
//    }
//
//    @GetMapping("/redis")
//    public Map redis_test(@RequestParam String key, @RequestParam String value) {
//        redisTemplate.opsForValue().set(key, value);
//        String value1 = (String) redisTemplate.opsForValue().get("key");
//        HashMap<String, Object> responseInfo = new HashMap<>();
//        HashMap<String, Object> responseData = new HashMap<>();
//        responseData.put("info", "Redis is ok!");
//        responseInfo.put("code", 20000);
//        responseInfo.put("msg", "Redis测试成功");
//        responseInfo.put("data", responseData);
//
//        logger.info("Redis is ok!");
//        return responseInfo;
//    }
//}

